package net.openl10n.mdf;

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

import net.openl10n.mdf.immutable.ImmutableBuilder;
import net.openl10n.mdf.immutable.ImmutableStateException;

public class ContainerBuilder extends AbstractDocumentPartBuilder implements ImmutableBuilder<Container>{

	private List<PartId> children;
	
	public ContainerBuilder() {
		children = new ArrayList<PartId>();
	}
	
	public ContainerBuilder(Container container) {
		super(container);
		children = new ArrayList<PartId>(container.getChildren());
	}
	
	public List<PartId> getChildren() {
		return children;
	}
	
	public void setChildren(List<PartId> children) {
		this.children = children;
	}
	
	public boolean isValid() {
		if(! super.isValid()) return false;
		return children != null;
	}
	
	public void validate() throws ImmutableStateException {
		if(! isValid()) {
			Set<String> invalidFields = new LinkedHashSet<String>();
			super.populateInvalidFields(invalidFields);
			if(children == null)
				invalidFields.add("children");
			throw new ImmutableStateException(invalidFields);
		}
	}
	
	@Override
	public Container build() throws ImmutableStateException{
		validate();
		return new Container(id, version, children);
	}
}
